package com.voc.cpnc.hook_alipay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.voc.cpnc.utils.JsonUtils;
import com.voc.cpnc.utils.LogUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class AliPayHook {

    private Context mContext;

    private String testString = "18396125445";

    private XC_LoadPackage.LoadPackageParam lpparam;

    public AliPayHook(XC_LoadPackage.LoadPackageParam lpparam) {
        this.lpparam = lpparam;
    }

    public void hookMainActivity() {
        XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                mContext = (Context) param.thisObject;
                LogUtils.e("Activity onCreate1：" + mContext.getClass().getName());
            }
        });

        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.ui.AliuserGuideActivity", lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                LogUtils.e("=====================start");

                Class<?> RecommendLoginUtil = XposedHelpers.findClassIfExists("com.ali.user.mobile.login.recommandlogin.utils.RecommendLoginUtil", lpparam.classLoader);

//                LogUtils.e("=====================1");
//                Class<?> bclass = XposedHelpers.findClassIfExists("com.ali.user.mobile.login.recommandlogin.utils.RecommendLoginUtil$RecommendRpcCallback", mContext.getClassLoader());
//
//                for (Method method : bclass.getMethods()) {
//                    LogUtils.e("=====" + method.getName());
//                }

//                LogUtils.e(Proxy.isProxyClass(bclass) + "=====================2" + bclass.getName());
//
//                Object bobject = Proxy.newProxyInstance(bclass.getClassLoader(), new Class[]{bclass}, new InvocationHandler() {
//                    @Override
//                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                        LogUtils.e("method：" + method.getName());
//                        return null;
//                    }
//                });
//                LogUtils.e("=====================3");
//                Object object = XposedHelpers.newInstance(RecommendLoginUtil);
//                LogUtils.e("=====================4");
//                XposedHelpers.callMethod(object, "setRecommendCallback", new Class[]{bclass}, bobject);

//                Object object = XposedHelpers.newInstance(RecommendLoginUtil);
//                XposedHelpers.callMethod(object, "doRecommendRpc", new Class[]{String.class}, "15260925089");

                LogUtils.e("=====================end");
            }
        });


        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.RecommandLoginActivity", lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：" + param.args[0]);
            }

            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                Activity activity = (Activity) param.thisObject;
                Bundle bundle = activity.getIntent().getExtras();
                bundle.putString("accountBetweenView", "18396125445");
                activity.getIntent().putExtra("accountBetweenView", "18396125445");
                LogUtils.e("afterHookedMethod：" + bundle.toString());
            }
        });

        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.view.LoginView", lpparam.classLoader, "checkAction", new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("afterHookedMethod：================1");
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.view.LoginView", lpparam.classLoader, "onNewData", Bundle.class, new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                Bundle bundle = (Bundle) param.args[0];
//                Object object = bundle.getSerializable("login_param");
                LogUtils.e("afterHookedMethod：================2" + bundle.toString());
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.BaseRecommandLoginView", lpparam.classLoader, "a", String.class, new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：=========a=======" + param.args[0]);
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.BaseRecommandLoginView", lpparam.classLoader, "a", String.class, new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：=========a=======" + param.args[0]);
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.BaseRecommandLoginView", lpparam.classLoader, "onNewData", Bundle.class, new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                Bundle bundle = (Bundle) param.args[0];
                LogUtils.e("beforeHookedMethod：=========onNewData=======" + bundle.toString());
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.InputAccountView", lpparam.classLoader, "onClick", View.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                View view = (View) param.args[0];
                LogUtils.e("beforeHookedMethod：========onClick========" + view.getId());
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.InputAccountView", lpparam.classLoader, "onNewData", Bundle.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(final MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);


                Bundle bundle = (Bundle) param.args[0];
                LogUtils.e("beforeHookedMethod：=========InputAccountView onNewData=======" + bundle.toString());

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(15000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Object object = XposedHelpers.getObjectField(param.thisObject, "e");
                        testString = "18060478847";
                        XposedHelpers.callMethod(object, "doRecommendRpc", new Class[]{String.class}, "");
                        LogUtils.e("==============执行啦");
                    }
                }).start();
            }
        });
        XposedHelpers.findAndHookMethod("com.alipay.mobile.antui.dialog.AUBaseDialog", lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：=========AUBaseDialog onCreate=======");
            }
        });
        XposedHelpers.findAndHookMethod("com.alipay.mobile.antui.dialog.AUBaseDialog", lpparam.classLoader, "show", new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：=========show=======");
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

//                Dialog dialog = (Dialog) param.thisObject;
//                dialog.cancel();
            }
        });

        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.utils.RecommendLoginUtil", lpparam.classLoader, "doRecommendRpc", String.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：========a========" + param.args[0]);
            }

            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                LogUtils.e("afterHookedMethod：========a========" + param.args[0]);
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.RecommandLoginActivity", lpparam.classLoader, "showTransParentDialog", new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
//                View view = (View) XposedHelpers.getObjectField(param.thisObject,"e");
                LogUtils.e("beforeHookedMethod：========showTransParentDialog========");
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                XposedHelpers.callMethod(param.thisObject,"hideTransParentDialog");
            }
        });

//        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.RecommandLoginActivity", lpparam.classLoader, "showTransParentDialog", new XC_MethodReplacement() {
//            @Override
//            protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {
//                return null;
//            }
//        });

        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.RecommandLoginActivity", lpparam.classLoader, "hideTransParentDialog", new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
//                View view = (View) XposedHelpers.getObjectField(param.thisObject,"e");
                LogUtils.e("beforeHookedMethod：========hideTransParentDialog========");
            }
        });

        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.InputAccountView", lpparam.classLoader, "getLoginAccount", new XC_MethodHook() {

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                param.setResult(testString);
                LogUtils.e("beforeHookedMethod：========getLoginAccount========" + param.getResult());
            }
        });
        XposedHelpers.findAndHookMethod("com.ali.user.mobile.login.recommandlogin.view.BaseRecommandLoginContainer", lpparam.classLoader, "getContainerInfo", String.class, new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("beforeHookedMethod：========getContainerInfo========" + param.args[0]);
            }
        });
    }

}
